<?php

namespace app\admin\controller\finance;

use app\common\controller\Backend;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;
use app\common\model\Config;
use app\admin\model\Middleman;
use app\admin\model\ShopFollow;
use app\admin\model\ShopPlatform;
use app\admin\model\Admin;
use app\admin\model\Play;
use app\common\model\OrderProof;

/**
 * 店铺管理
 *
 */
class MiddlemanPerformance extends Backend
{
    /**
     * Play模型对象
     * @var \app\admin\model\Play
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        // $this->model = new \app\admin\model\Play;
        $this->model = new \app\admin\model\Shop;
        $salesmanList = [];
        $salesman = $this->salesmanList();
        foreach ($salesman as $key => $value) {
            $salesmanList[$value['id']] = $value['nickname'];
        }
        $this->assignconfig(['salesmanList' => $salesmanList]);
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            // 有已完成的活动订单
            $filter = $_GET['filter'];
            $filterarr = [];
            if ($filter) {
                $filterarr = json_decode($filter,1);
            }
            $where2 = 'm.no=0';
            $auth_group_access = Db::name('auth_group_access')->alias('aga')->where(['aga.uid'=>$this->auth->id])->find();
            $groups = [];
            if ($auth_group_access['group_id']==3) { // 业务员
                $groups[] = $this->auth->id;
            } elseif ($auth_group_access['group_id']==5) { // 经理
                $groups[] = $this->auth->id;
                $admin = Db::name('admin')->where(['leader'=>$this->auth->id])->find();
                foreach ($admin as $key => $value) {
                    $groups[] = $value['id'];
                }
            }
            if ($groups) {
                $where2 .= ' and m.salesman_admin_id in ('.implode(',', $groups).')';
            }
            if (isset($filterarr['nickname'])) {
                $where2 .= ' and m.salesman_admin_id='.$filterarr['nickname'];
            }
            if (isset($filterarr['start_date'])) {
                $date = explode(' - ', $filterarr['start_date']);
                $where2 .= ' and p.create_dt >"'.$date[0].'" and p.create_dt<"'.$date[1].'"';
            }
            if (isset($filterarr['city'])) {
                $where2 .= ' and s.city='.$filterarr['city'];
            }
            $data = Db::name('middleman')
                ->alias('m')
                ->field('m.*,p.shop_name,p.play_no,p.start_date,p.service_amt,s.city,p.id as pid')
                ->join('chbc_shop s','m.id=s.middleman_id')
                ->join('chbc_play p','s.shop_no=p.shop_no and super_play_no=0')
                ->join('chbc_play_order po','po.play_no=p.play_no and order_status in (3,-7,-8,-9)')
                ->where($where2)
                ->group('p.play_no')
                ->order('m.id DESC,pid DESC')
                ->paginate($limit);
            $list = [];
            foreach ($data as $key => $row) {
                $list[$key] = $row;
            }
            $admin = Db::name('auth_group_access')->alias('aga')->join('chbc_admin a','aga.uid=a.id')->where(['aga.group_id'=>3])->select();
            $nickname = [];
            foreach ($admin as $v) {
                $nickname[$v['id']] = $v['nickname'];
            }
            foreach ($list as &$value) {
                $amount = (int)Db::name('play_order')->where(['play_no'=>$value['play_no'],'order_status'=>['in',[3,-7,-8,-9]]])->count();
                $value['amount'] = $amount;
                $value['yc'] = (int)Db::name('play_order')->where(['play_no'=>$value['play_no'],'order_status'=>['in',[-4]]])->count();
                $value['money'] = round($amount*$value['rebate_amt'],2);
                $value['nickname'] = isset($nickname[$value['salesman_admin_id']])?$nickname[$value['salesman_admin_id']]:'';
            }
            $result = array("total" => $data->total(), "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

}
